import re
import webbrowser

from kivy.uix.textinput import TextInput

from amgineirc.view.messageView import MessageView

class SendView(TextInput, MessageView):
    """
    in this view the user enters text to be sent to the target
    """
    def __init__(self, **kargs):
        # add default settings to kargs
        k_args = self._set_kargs(**kargs)
        k_args['multiline'] = False
        k_args['text'] = u''
        k_args['use_bubble'] = True
        TextInput.__init__(self, **k_args)
        # self.bind(text=self.on_send)
        TextInput.bind(self,
                       on_text_validate=self.w_on_text_validate_handler)
        MessageView.__init__(self)
        self._on_send_handler = lambda *args : None

    def bind(self, **kargs):
        # overridden for 'on_text_validate'
        k_args = self._set_kargs(**kargs)
        TextInput.bind(self, **k_args)

    # view model methods

    def vm_set_state(self, **kargs):
        # if 'connected' in kargs:
        # show connected
        pass
    
    def vm_bind_on_send(self, func):
        # func should accept one arg - value
        self._on_send_handler = func

    # widget methods
    
    def w_on_text_validate_handler(self, instance):
    # def w_on_text_validate_handler(self, instance, value):
        # instance = self
        value = instance.text.replace('\r\n', ' ') \
                .replace('\n', ' ') \
                .replace('\r', ' ')
        if len(value) > 0:
            self.text = u''
            self._on_send_handler(value)

    # private methods

    def _set_kargs(self, **kargs):
        if 'multiline' in kargs:
            # ignore
            del(kargs['multiline'])
        if 'on_text_validate' in kargs:
            # use self._on_send_handler
            self._on_send_handler = kargs['on_text_validate']
            del(kargs['on_text_validate'])
        return kargs
